﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DingY.DB;

namespace DotNet.WinUI.Customer
{
    public partial class FrmIndustryEdit : BaseForm
    {
        public FrmIndustryEdit()
        {
            InitializeComponent();
        }

        private string IndustryID = "0";
        public FrmIndustryEdit(string industryID)
        {
            InitializeComponent();
            
            IndustryID = industryID;

            if (industryID != "0")
            {
                string sql = @"SELECT *
                    FROM Industry
                    WHERE IndustryID = '{0}' ";
                sql = string.Format(sql, IndustryID);

                SqlDB objSqlDB = new SqlDB();
                DataSet ds = objSqlDB.ExecuteDataSet(sql);
                DataRow row = ds.Tables[0].Rows[0];
                txtDepict.Text = row["Depict"].ToString();
                txtName.Text = row["Name"].ToString();
            }
        }
        
        private void SaveData()
        {
            try
            {
                if (txtName.Text.Length <= 0)
                {
                    ShowMsg("请输入名称！");
                    return;
                }

                string _Sql = null;
                SqlDB objSqlDB = new SqlDB();
                if (IndustryID == "0")
                {
                    _Sql = @"SELECT Count(Name) FROM Industry WHERE Name ='{0}' ";
                    _Sql = string.Format(_Sql, txtName.Text);

                    if (Convert.ToInt32(objSqlDB.ExecuteScalar(_Sql)) >= 1)
                    {
                        ShowMsg("名称已经存在，请重新输入！");
                        txtName.Focus();
                        return;
                    }

                    _Sql = string.Format(@"INSERT INTO Industry(Name, Depict)
                        VALUES ('{0}', '{1}') ",
                        txtName.Text,
                        txtDepict.Text);
                }
                else
                {
                    _Sql = @"UPDATE Industry
                        SET Name ='{1}', 
                        Depict ='{2}'
                        WHERE IndustryID ='{0}' ";
                    _Sql = string.Format(_Sql,
                        IndustryID,
                        txtName.Text,
                        txtDepict.Text);
                }
                if (objSqlDB.ExecuteSql(_Sql) >= 1)
                {
                    ShowMsg("数据保存成功!");
                }
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message);
            }
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Name.ToLower())
            {
                case "save":
                    SaveData();
                    break;
                case "close":
                    Close();
                    break;
            }
        }
    }
}